﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace IntelligentPharmacy.publicclass
{
    public  class ConfigManager
    {
        JavaScriptSerializer jsonObject = new JavaScriptSerializer();
        System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);
        /// <summary>
        /// 添加键值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddAppSetting(string key, string value)
        {
            config.AppSettings.Settings.Add(key, value);
            config.Save();
        }

        /// <summary>
        /// 修改键值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SaveAppSetting(string key, string value)
        {
            config.AppSettings.Settings.Remove(key);
            config.AppSettings.Settings.Add(key, value);
            config.Save();
        }

        /// <summary>
        /// 获得键值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetAppSetting(string key)
        {
            return config.AppSettings.Settings[key].Value;
        }

        /// <summary>
        /// 移除键值
        /// </summary>
        /// <param name="key"></param>
        public void DelAppSetting(string key)
        {
            config.AppSettings.Settings.Remove(key);
            config.Save();
        }
        /// <summary>
        /// 读frid标签个数
        /// </summary>
        public Dictionary<string,string> readRfidConfig()
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            string json=config.AppSettings.Settings["frid"].Value;
            Dictionary<string, object> JsonData = (Dictionary<string, object>)jsonObject.DeserializeObject(json);
            if (JsonData == null)
                return new Dictionary<string, string>();
            foreach (KeyValuePair<string,object> item in JsonData)
            {
                data.Add(item.Key, (string)item.Value);
            }
            return data;
        }
        /// <summary>
        /// 写frid标签
        /// </summary>
        public void writeFridConfig(Dictionary<string, string> value)
        {
            string json=JsonToObject.Serialize(value);
            config.AppSettings.Settings.Remove("frid");
            config.AppSettings.Settings.Add("frid", json);
            config.Save();
        }
        public void updataFridConfig(Dictionary<string, string> value)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            string json = config.AppSettings.Settings["frid"].Value;
            Dictionary<string, object> JsonData = (Dictionary<string, object>)jsonObject.DeserializeObject(json);
            foreach (KeyValuePair<string, object> item in JsonData)
            {
                data.Add(item.Key, (string)item.Value);
            }
            foreach(KeyValuePair<string,string> keyValue in value)
            {
                if (data.ContainsKey(keyValue.Key))
                {
                    data.Remove(keyValue.Key);
                }
            }
            writeFridConfig(data);
        }
    }
}
